package com.yidusoft.controller;

import com.yidusoft.core.Result;
import com.yidusoft.core.ResultGenerator;
import com.yidusoft.domain.SecOrg;
import com.yidusoft.service.SecOrgService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yidusoft.utils.CodeHelper;
import com.yidusoft.utils.TreeBuilder;
import com.yidusoft.utils.TreeNode;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.Model;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.*;

/**
* Created by CodeGenerator on 2017/07/18.
*/
@Controller
@RequestMapping("/sec/org")
public class SecOrgController {
    @Resource
    private SecOrgService secOrgService;


    @PostMapping("/tree")
    @ResponseBody
    public List<TreeNode> tree() {
        List<TreeNode> nodes =secOrgService.getTree();
        return TreeBuilder.buildByRecursive(nodes);
    }


    @RequestMapping(value ="/linkAdd")
    public String linkAdd(Model model){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("UUID",UUID.randomUUID().toString());
        map.put("createDate",new Date());
        model.addAttribute("infos",map);
        return  "secorg/secorg-add";
    }

    @RequestMapping(value ="/orgLoad")
    public String orgLoad(Model model){
        return  "secorg/load-user";
    }

    @RequestMapping(value ="/linkList")
    public String linkList(){
        return  "secorg/secorg-list";
    }

    @RequestMapping(value ="/linkUpdate")
    public String linkUpdate(Model model,String id){
        SecOrg secOrg = secOrgService.findById(id);
        model.addAttribute("secOrg",secOrg);
        return  "secorg/secorg-update";
    }

    @PostMapping("/add")
    @ResponseBody
    public Result add(SecOrg secOrg) {
        secOrg.setId(UUID.randomUUID().toString());
        secOrg.setOrgCode(CodeHelper.getCode("ZZ"));
        secOrgService.save(secOrg);
        return ResultGenerator.genSuccessResult();
    }

   //把数据永久删除
    @PostMapping("/deleteTrue")
    @ResponseBody
    public Result deleteTrue(String  id) {
        secOrgService.deleteById(id);
        return ResultGenerator.genSuccessResult();
    }

    //逻辑删除，数据还在
    @PostMapping("/delete")
    @ResponseBody
    public Result delete(String  id) {
        SecOrg secOrg = secOrgService.findById(id);
        secOrg.setDeleted(1);
        secOrgService.update(secOrg);
        return ResultGenerator.genSuccessResult();
    }


    //逻辑删除，数据还在 批量删除
    @PostMapping("/deleteBacth")
    @ResponseBody
    public Result deleteBacth(String  ids) {
        String arr [] = ids.split(",");
        for(String str : arr){
            SecOrg secOrg = secOrgService.findById(str);
            secOrg.setDeleted(1);
            secOrgService.update(secOrg);
        }
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    @ResponseBody
    public Result update(@Valid SecOrg secOrg) {
        secOrgService.update(secOrg);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/detail")
    @ResponseBody
    public Result detail(String id) {
        SecOrg secOrg = secOrgService.findById(id);
        return ResultGenerator.genSuccessResult(secOrg);
    }


    @PostMapping("/list")
    @ResponseBody
    public Map<String,Object> list() {
        Map<String,Object> map = new HashMap<String,Object>();
        List<SecOrg> list = secOrgService.findAll();
        map.put("Rows",list);
        map.put("Total",list.size());
        return map;
    }


    @PostMapping("/listByPage")
    @ResponseBody
    public Result listByPage(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<SecOrg> list = secOrgService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }
}
